package com.campus.help.controller;

import com.campus.help.dto.ApiResponse;
import com.campus.help.dto.UserLoginRequest;
import com.campus.help.dto.UserRegisterRequest;
import com.campus.help.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {
    
    private final UserService userService;
    
    /**
     * 用户注册
     * @param request 注册请求
     * @return 注册结果
     */
    @PostMapping("/register")
    public ApiResponse<String> register(@Valid @RequestBody UserRegisterRequest request) {
        try {
            String result = userService.register(request);
            return ApiResponse.success(result);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 用户登录
     * @param request 登录请求
     * @return 登录结果
     */
    @PostMapping("/login")
    public ApiResponse<String> login(@Valid @RequestBody UserLoginRequest request) {
        try {
            String result = userService.login(request);
            return ApiResponse.success(result);
        } catch (Exception e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    
    /**
     * 检查用户是否存在
     * @param studentId 学号
     * @return 检查结果
     */
    @GetMapping("/exists/{studentId}")
    public ApiResponse<Boolean> userExists(@PathVariable String studentId) {
        boolean exists = userService.existsByStudentId(studentId);
        return ApiResponse.success(exists);
    }
}